﻿using System;
using System.Linq.Expressions;

using Bootstrap5.Abstractions;
using Bootstrap5.Components.Forms;

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Rendering;

namespace Bootstrap5;

[HtmlTag("label")]
[CssClass("form-label")]
public class FormLabel<TValue>:BootstrapChildContentWithChildCascadingComponentBase<FormLabel<TValue>,Form>
{
	[Parameter] public Expression<Func<TValue>> For { get; set; }

	protected override void BuildRenderTree(RenderTreeBuilder builder)
	{
		builder.OpenElement(0,GetHtmlTagName());
		BuildCommonAttributes(builder);
		builder.AddAttribute(10, "for", FieldIdentifier.Create(For).FieldName);
		
		if(ChildContent is null && For.TryGetDisplayName<TValue>(out string value))
		{
			builder.AddContent(20,value);
		}
		else
		{
			BuildChildContent(builder);
		}

		builder.CloseElement();
	}

}
